package 算法.test;

import java.util.List;
import java.util.Random;
import java.util.Vector;

/**
 * 选择排序
 * 
 * @author mantou
 *
 */
public class Select {
	/**
	 * 选择排序
	 * 
	 * @param array
	 *            待排序的数组 -[从小到大]
	 * @return
	 */
	public static int[] sort(int[] array) {
		for (int i = 0; i < array.length; i++) {
			int min = i;
			for (int j = i + 1; j < array.length; j++) {
				if (array[j] < array[min]) {
					min=j;
				}
			}
			int temp =array[i];
			array[i]=array[min];
			array[min]=temp;
		}
		return array;
	}

	public static String getArrayStr(int[] array) {
		String str = "";
		for (int i = 0; i < array.length; i++) {
			str += array[i] + " ";
		}
		return str;
	}

	public static void main(String[] args) {
		int arrayCount = 10000;
		int[] array = new int[arrayCount];
		long average = 0;
		int m = 0;
		int forCount = 10;
		while (m < forCount) {
			for (int i = 0; i < array.length; i++) {
				array[i] = new Random().nextInt(arrayCount);
			}
			System.out.println("排序前-->" + getArrayStr(array));
			long startTime = System.currentTimeMillis();
			sort(array);
			long endTime = System.currentTimeMillis();
			System.out.println("排序后-->" + getArrayStr(array));
			System.out.println("排序时间-->" + (endTime - startTime) + "ms");
			average += (endTime - startTime);
			m++;
		}
		System.out.println("平均耗时->" + (average / m) + "ms");
	}
}
